<?php

namespace app\api\controller;


use app\api\biz\UserRoleBiz;
use app\common\controller\ApiController;
use app\common\Result;
use joncc\attribute\attr\AuthNeedless;
use joncc\attribute\attr\Comment;
use joncc\attribute\attr\Params;
use joncc\attribute\attr\PermissionDefine;
use joncc\attribute\attr\PermissionNeedless;
use think\response\Json;


#[Comment('用户角色')]
#[AuthNeedless]
#[PermissionNeedless]
#[PermissionDefine('list', '列表')]
#[PermissionDefine('add', '新增')]
class UserRole extends ApiController
{

    #[Params('user_id', Params::INT, true, '用户ID')]
    public function list(): Json
    {
        $userRoleBiz = UserRoleBiz::list(p('user_id'));
        return Result::success($userRoleBiz::getData());
    }

    #[Params('user_id', Params::INT, true, '用户ID')]
    #[Params('role_ids', Params::ARRAY, false, '角色IDs')]
    public function add(): Json
    {
        $userRoleBiz = UserRoleBiz::add(p('user_id'), p('role_ids'));
        if ($userRoleBiz::hasError()) {
            return Result::fail(...$userRoleBiz::getError());
        }
        return Result::success($userRoleBiz::getData());
    }
}
